package com.situ.javase.day12;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

import org.junit.Test;

public class IODemo {
	public static void main(String[] args) {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader("io11.txt");
			// 读取单个字符,如果已到达流的末尾，则返回 -1
			int ch1 = fileReader.read();
			System.out.println(ch1);// 97
			System.out.println((char)ch1);// a
			int ch2 = fileReader.read();
			System.out.println((char)ch2);// b
			int ch3 = fileReader.read();
			System.out.println((char)ch3);// c
			int ch4 = fileReader.read();
			System.out.println(ch4);// -1
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileReader != null) {
				try {
					// The local variable fileReader may not have been initialized
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Test
	public void testFileReader() {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader("io.txt");
			// 用while循环去读，直到返回的值是-1，就退出循环
			int ch = -1;
			while ((ch = fileReader.read()) != -1) {
				System.out.println((char)ch);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void testFileReader2() {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader("io.txt");
			// public int read(char[] cbuf) 
			// 将字符读入数组 buffer
			// 1、没有到达流的末尾，返回的是读取的字符个数
			// 2、如果已到达流的末尾，则返回 -1
			char[] buffer = new char[10];
			int length = 0;
			while ((length = fileReader.read(buffer)) != -1) {
				System.out.println("length: " + length);
				System.out.println(Arrays.toString(buffer));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void fileCopy() {
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		try {
			fileReader = new FileReader("io.txt");
			fileWriter = new FileWriter("io_back.txt");
			char[] buffer = new char[10];
			int length = 0;
			while ((length = fileReader.read(buffer)) != -1) {
				System.out.println("length: " + length);
				System.out.println(Arrays.toString(buffer));
				// 读出多少就写多少，最后一次读取的数据长度可能不够
				// buffer数组长度
				fileWriter.write(buffer, 0, length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileWriter != null) {
				try {
					fileWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void fileInputOutputStreamCopy() {
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			fileInputStream = new FileInputStream("bd.png");
			fileOutputStream = new FileOutputStream("bd_back.png");
			byte[] buffer = new byte[1024];
			int length = 0;
			while ((length = fileInputStream.read(buffer)) != -1) {
				fileOutputStream.write(buffer, 0, length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	
}
